﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit.Electric.Common;
using HYRElectric.WirePlace;

namespace HYRElectric.Equipment
{
	public partial class DlgLayCrossWall : System.Windows.Forms.Form
	{
		public DlgLayCrossWall(Document document)
		{
			this.InitializeComponent();
			this.doc = document;
		}

		private void DlgLayCrossWall_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			try
			{
				this.xmlHelper.LoadXml(this.path, "Root");
			}
			catch (Exception)
			{
				File.Delete(this.path);
			}
			this.LoadDlgData();
			this.InitData();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelRevitOperate()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgLayCrossWall.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgLayCrossWall.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void DlgLayCrossWall_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.GetData();
			this.SaveDlgData();
			this.CancelRevitOperate();
			this.SetFocusToRevit();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetFocusToRevit()
		{
			int wMsg = 7;
			DlgLayCrossWall.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void DlgLayCrossWall_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SetFocusToRevit();
				this.GetData();
				return;
			}
			base.Focus();
		}

		private void SetStatusLayStyle(bool bDoubleSingle)
		{
			this.radioButtonDoubleSide.Checked = !bDoubleSingle;
			this.radioButtonSingleSide.Checked = bDoubleSingle;
		}

		public void InitData()
		{
			this.textBoxWireName.Text = this.m_laySet.WireName;
			this.numericUpDownWireNO.Value = this.m_laySet.WireNO;
			this.checkBoxDrawWire.Checked = this.m_laySet.DrawWire;
			this.SetStatusLayStyle(this.m_laySet.LayType == 1);
		}

		private void GetData()
		{
			this.m_laySet.WireName = this.textBoxWireName.Text;
			this.m_laySet.WireNO = (int)this.numericUpDownWireNO.Value;
			this.m_laySet.DrawWire = this.checkBoxDrawWire.Checked;
			this.m_laySet.LayType = (this.radioButtonDoubleSide.Checked ? 0 : 1);
		}

		public void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire", "LayWallCrossSet"));
			string text = this.xmlHelper.GetAttribute("WireName");
			string value = this.xmlHelper.GetAttribute("WireNO");
			string value2 = this.xmlHelper.GetAttribute("LayType");
			string value3 = this.xmlHelper.GetAttribute("DrawWire");
			if (string.IsNullOrEmpty(text))
			{
				text = "WL";
			}
			if (string.IsNullOrEmpty(value))
			{
				value = "1";
			}
			if (string.IsNullOrEmpty(value2))
			{
				value2 = "0";
			}
			if (string.IsNullOrEmpty(value3))
			{
				value3 = "1";
			}
			this.m_laySet.WireName = text;
			this.m_laySet.WireNO = Convert.ToInt32(value);
			this.m_laySet.LayType = Convert.ToInt32(value2);
			this.m_laySet.DrawWire = (Convert.ToInt32(value3) != 0);
		}

		public void SaveDlgData()
		{
			try
			{
				this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire", "LayWallCrossSet"));
				this.xmlHelper.SetAttribute("WireName", this.m_laySet.WireName);
				this.xmlHelper.SetAttribute("WireNO", this.m_laySet.WireNO.ToString());
				this.xmlHelper.SetAttribute("LayType", this.m_laySet.LayType.ToString());
				this.xmlHelper.SetAttribute("DrawWire", (this.m_laySet.DrawWire ? 1 : 0).ToString());
				this.xmlHelper.SaveXml("");
			}
			catch
			{
			}
		}

		private void btnWireParameter_Click(object sender, EventArgs e)
		{
			new NewWireParamForm(this.doc).ShowDialog();
		}

		private void radioButtonDoubleSide_CheckedChanged(object sender, EventArgs e)
		{
		}

		private void radioButtonSingleSide_CheckedChanged(object sender, EventArgs e)
		{
		}

		private void numericUpDownWireNO_ValueChanged(object sender, EventArgs e)
		{
            string.IsNullOrEmpty((sender as System.Windows.Forms.Control).Text);
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CrossWallWireSet.xml");

		private Document doc;

		private const string Root = "Root";

		private const string Node = "HYConnectWire";

		private const string LayWallCrossSet = "LayWallCrossSet";

		private const string WireName = "WireName";

		private const string WireNO = "WireNO";

		private const string LayType = "LayType";

		private const string DrawWire = "DrawWire";

		public LayWallCrossDefine m_laySet = new LayWallCrossDefine();
	}
}
